package com.sisyphus.mock.controllers;

import com.sisyphus.mock.data.model.BaseResponse;
import com.sisyphus.mock.utils.FileUtil;
import com.sisyphus.mock.utils.Logger;
import com.yanzhenjie.andserver.annotation.PostMapping;
import com.yanzhenjie.andserver.annotation.RequestMapping;
import com.yanzhenjie.andserver.annotation.RequestParam;
import com.yanzhenjie.andserver.annotation.RestController;
import com.yanzhenjie.andserver.http.multipart.MultipartFile;
import com.yanzhenjie.andserver.util.Executors;

import java.io.File;
import java.io.IOException;

@RestController
@RequestMapping(path = "/file")
public class FileController {

    /**
     * 上传图片文件
     */
    @PostMapping(path = "/upload")
    BaseResponse<String> upload(@RequestParam(name = "avatar") final MultipartFile file) {
        final File localFile = FileUtil.createRandomFile(file);

        Executors.getInstance().submit(() -> {
            try {
                file.transferTo(localFile);
                Logger.i(localFile.getPath());
                Logger.i(localFile.getAbsoluteFile());
            } catch (IOException e) {
                e.printStackTrace();
            }
        });

        Logger.i(localFile.getPath() + "\n" + localFile.getAbsoluteFile());

        return BaseResponse.success(localFile.getPath());
    }

}
